home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
elv18src.zip
/
ref.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-17
|
13KB
|
579 lines
/* ref2.c */
/* This is a totally rewritten version of ref. This version looks for the
* desired function name in the "tags" file, and then reads the header out
* from the source file. There is no longer any need for a "refs" file.
*
* Usage: ref [-t] [-x] [-f file] [-c class] tag
* Options: -t output tag info, not the description
* -x require exact match
* -f file default filename for static functions
* -c class default class names for class functions
*/
#ifdef __STDC__
# include <string.h>
# include <stdlib.h>
#endif
#include <stdio.h>
#include "config.h"
extern char *cktagdir P_((char *, char *));
extern int getline P_((char *, int, FILE *));
extern int lookup P_((char *, char *));
extern int find P_((char *));
extern void usage P_((void));
extern int countcolons P_((char *));
extern void main P_((int, char **));
/* This is the default path that is searched for tags */
#if OSK
# define DEFTAGPATH ".:/dd/defs:/dd/defs/sys:/dd/usr/src/lib:../lib:/dd/usr/lib"
#else
# if ANY_UNIX
# define DEFTAGPATH ".:/usr/include:/usr/include/sys:/usr/src/lib:../lib:/usr/local/lib"
# else
# if MSDOS || TOS || OS2
# define DEFTAGPATH ".;C:\\include;C:\\include\\sys;C:\\lib;..\\lib"
# define SEP ';'
# else
# if AMIGA
# define DEFTAGPATH ".;Include:;Include:sys"
# define SEP ';'
# else /* any other OS */
# define DEFTAGPATH "."
# endif
# endif
# endif
#endif
#ifndef SEP
# define SEP ':'
#endif
/* These variables reflect the command-line options given by the user. */
int taginfo; /* boolean: give only the tag info? (not header?) */
int exact; /* boolean: require exact match? */
char *def_file; /* default filename for static functions */
char *def_class; /* default classname for class members */
int colons; /* #colons in tag: 0=normal, 1=static, 2=member */
/* This function checks for a tag in the "tags" file of given directory.
* If the tag is found, then it returns a pointer to a static buffer which
* contains the filename, a tab character, and a linespec for finding the
* the tag. If the tag is not found in the "tags" file, or if the "tags"
* file cannot be opened or doesn't exist, then this function returns NULL.
*/
char *cktagdir(tag, dir)
char *tag; /* name of the tag to look for */
char *dir; /* name of the directory to check */
{
char buf[BLKSIZE];
static char found[BLKSIZE];
FILE *tfile;
int len;
char *scan;
#if AMIGA
if (dir[strlen(dir) - 1] == COLON)
sprintf(buf, "%s%s", dir, TAGS); /* no slash after colon. */
else
#endif
/* construct the name of the "tags" file in this directory */
sprintf(buf, "%s%c%s", dir, SLASH, TAGS);
/* Try to open the tags file. Return NULL if can't open */
#if AMIGA
if (buf[0] == '.' && buf[1] == SLASH)
tfile = fopen(&buf[2], "r");
else
#endif
tfile = fopen(buf, "r");
if (!tfile)
{
return (char *)0;
}
/* compute the length of the tagname once */
len = strlen(tag);
/* read lines until we get the one for this tag */
found[0] = '\0';
while (fgets(buf, sizeof buf, tfile))
{
/* is this the one we want? */
if (!strncmp(buf, tag, len) && buf[len] == '\t')
{
/* we've found a match -- remember it */
strcpy(found, buf);
/* if there is no default file, or this match is in
* the default file, then we've definitely found the
* one we want. Break out of the loop now.
*/
if (!def_file || !strncmp(&buf[len + 1], def_file, strlen(def_file)))
{
break;
}
}
/* does it maybe match the stuff after the colons? */
if (!exact)
{
scan = strchr(buf, '\t');
if (scan
&& (int)(scan - buf) > len
&& scan[-len - 1] == ':'
&& !strncmp(tag, scan - len, len))
{
strcpy(found, buf);
len = (int)(scan - buf);
break;
}
}
}
/* we're through reading */
fclose(tfile);
/* if there's anything in found[], use it */
if (found[0])
{
return &found[len + 1];
}
/* else we didn't find it */
return (char *)0;
}
/* This function reads a single textline from a binary file. It returns
* the number of bytes read, or 0 at EOF.
*/
int getline(buf, limit, fp)
char *buf; /* buffer to read into */
int limit; /* maximum characters to read */
FILE *fp; /* binary stream to read from */
{
int bytes; /* number of bytes read so far */
int ch; /* single character from file */
for (bytes = 0, ch = 0; ch != '\n' && --limit > 0 && (ch = getc(fp)) != EOF; bytes++)
{
#if MSDOS || TOS || OS2
/* since this is a binary file, we'll need to manually strip CR's */
if (ch == '\r')
{
continue;
}
#endif
*buf++ = ch;
}
*buf = '\0';
return bytes;
}
/* This function reads a source file, looking for a given tag. If it finds
* the tag, then it displays it and returns TRUE. Otherwise it returns FALSE.
* To display the tag, it attempts to output any introductory comment, the
* tag line itself, and any arguments. Arguments are assumed to immediately
* follow the tag line, and start with whitespace. Comments are assumed to
* start with lines that begin with "/ *", "//", "(*", or "--", and end at the
* tag line or at a blank line.
*/
int lookup(dir, entry)
char *dir; /* name of the directory that contains the source */
char *entry; /* source filename, <Tab>, linespec */
{
char *name; /* basename of source file */
char buf[BLKSIZE]; /* pathname of source file */
long lnum; /* desired line number */
long thislnum; /* current line number */
long here; /* seek position where current line began */
long comment; /* seek position of introductory comment, or -1L */
FILE *sfile; /* used for reading the source file */
int len; /* length of string */
int noargs = 0; /* boolean: don't show lines after tag line? */
char *ptr;
/* construct the pathname of the source file */
name = entry;
strcpy(buf, dir);
ptr = buf + strlen(buf);
#if AMIGA
if (ptr[-1] != COLON)
#endif
*ptr++ = SLASH;
while (*entry != '\t')
{
*ptr++ = *entry++;
}
*entry++ = *ptr = '\0';
/* searching for string or number? */
if (*entry >= '0' && *entry <= '9')
{
/* given a specific line number */
lnum = atol(entry);
entry = (char *)0;
noargs = 1;
}
else
{
/* given a string -- strip off "/^" and "$/\n" */
entry += 2;
len = strlen(entry) - 2;
if (entry[len - 1] == '$')
{
entry[len - 1] = '\n';
}
if (!strchr(entry, '('))
{
noargs = 1;
}
lnum = 0L;
}
/* Open the file. Note that we open the file in binary mode even
* though we know it is a text file, because ftell() and fseek()
* don't work on text files.
*/
#if MSDOS || TOS || OS2
sfile = fopen(buf, "rb");
#else
# if AMIGA
if (buf[0] == '.' && buf[1] == SLASH)
sfile = fopen(&buf[2], "r");
else
# endif
sfile = fopen(buf, "r");
#endif
if (!sfile)
{
/* can't open the real source file. Try "refs" instead */
#if AMIGA
if (dir[strlen(dir) - 1] == COLON)
sprintf(buf, "%srefs", dir);
else
#endif
sprintf(buf, "%s%crefs", dir, SLASH);
#if MSDOS || TOS || OS2
sfile = fopen(buf, "rb");
#else
# if AMIGA
if (buf[0] == '.' && buf[1] == SLASH)
sfile = fopen(&buf[2], "r");
else
# endif
sfile = fopen(buf, "r");
#endif
if (!sfile)
{
/* failed! */
return 0;
}
name = "refs";
}
/* search the file */
for (comment = -1L, thislnum = 0; here = ftell(sfile), thislnum++, getline(buf, BLKSIZE, sfile) > 0; )
{
/* Is this the start/end of a comment? */
if (comment == -1L)
{
/* starting a comment? */
if (buf[0] == '/' && buf[1] == '*'
|| buf[0] == '/' && buf[1] == '/'
|| buf[0] == '(' && buf[1] == '*'
|| buf[0] == '-' && buf[1] == '-')
{
comment = here;
}
}
else
{
/* ending a comment? */
if (buf[0] == '\n' || buf[0] == '#')
{
comment = -1L;
}
}
/* is this the tag line? */
if (lnum == thislnum || (entry && !strncmp(buf, entry, len)))
{
/* display the filename & line number where found */
if (strcmp(dir, "."))
printf("%s%c%s, line %ld:\n", dir, SLASH, name, thislnum);
else
printf("%s, line %ld:\n", name, thislnum);
/* if there were introductory comments, show them */
if (comment != -1L)
{
fseek(sfile, comment, 0);
while (comment != here)
{
getline(buf, B